package cc.douquan.com.doutu.weight;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.AttributeSet;
import android.webkit.GeolocationPermissions;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebStorage;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import cc.douquan.com.doutu.utils.StringUtils;


/**
 * Created by zhudongmei on 2016/7/8.
 */
public class MyProgressWebView extends WebView {
    private String APP_CACAHE_DIRNAME = "/gjg";
    private ProgressBar progressBar;
    private Context mContext;
    private OnWebViewListener mWebViewListener;
//    private OnWebViewReload mWebViewReload;

    public MyProgressWebView(Context context) {
        super(context);
        this.mContext = context;
        initView();
    }

    public MyProgressWebView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        progressBar = new ProgressBar(context, null, android.R.attr.progressBarStyleHorizontal);
        progressBar.setMax(100);
        progressBar.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 20, 0, 0));
        addView(progressBar);
        initView();
    }

    @SuppressLint({"SetJavaScriptEnabled", "JavascriptInterface"})
    private void initView() {
        WebSettings setting = getSettings();
        setting.setJavaScriptEnabled(true);
//        setting.setUseWideViewPort(true);
        setting.setDefaultTextEncodingName("UTF-8");
//        setting.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
//        setting.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
//        setting.setLoadWithOverviewMode(true);
//        setting.setAppCacheEnabled(true);
//        setting.setDomStorageEnabled(true);
//        setting.setGeolocationEnabled(true);
//        setting.setAllowFileAccess(true);
//        setting.setDatabaseEnabled(true);
//        String appCacheDir = mContext.getDir("cache", Context.MODE_PRIVATE).getPath();
//        setting.setAppCacheMaxSize(8 * 1024 * 1024);
//        setting.setAppCachePath(appCacheDir);
        setWebViewClient(mWebViewClient);
        setWebChromeClient(mWebChromeClient);
    }

    public WebViewClient mWebViewClient = new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            // TODO Auto-generated method stub
            //======================
            if (url.startsWith("tel:")) {
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                mContext.startActivity(intent);
            } else if (url.startsWith("activity:")) {
                String str = url.replaceAll("\\D+", "");
                if (mWebViewListener != null) {
                    mWebViewListener.onWebViewChange(str);
                }
            } else if (url.startsWith("weixin://wap/pay?")) {
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_VIEW);
                intent.setData(Uri.parse(url));
                mContext.startActivity(intent);
            } else {
                if (StringUtils.isUrl(url)) {
                    view.loadUrl(url);
                }
            }
            return true;
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
        }

        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            super.onReceivedError(view, errorCode, description, failingUrl);
        }

    };

    private WebChromeClient mWebChromeClient = new WebChromeClient() {
        //进度条
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            if (newProgress == 100) {
                progressBar.setVisibility(GONE);
            } else {
                if (progressBar.getVisibility() == GONE)
                    progressBar.setVisibility(VISIBLE);
                progressBar.setProgress(newProgress);
            }
            super.onProgressChanged(view, newProgress);
        }

        //扩充缓存的容量
        @Override
        public void onReachedMaxAppCacheSize(long spaceNeeded,
                                             long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
            quotaUpdater.updateQuota(spaceNeeded * 2);
        }

        public void onGeolocationPermissionsShowPrompt(String origin,
                                                       GeolocationPermissions.Callback callback) {
            callback.invoke(origin, true, false);
            super.onGeolocationPermissionsShowPrompt(origin, callback);
        }

    };

    public interface OnWebViewListener {
        public void onWebViewChange(String type);

    }

    public void setWebViewListener(OnWebViewListener listener) {
        mWebViewListener = listener;
    }

//    public interface OnWebViewReload {
//        public void onWebViewChange(boolean isFalse);
//
//    }
//
//    public void setWebViewReload(OnWebViewReload listener) {
//        mWebViewReload = listener;
//    }
}
